<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <input type="text" class="ipt" />

    <script>
      let ipt = document.querySelector(".ipt");

      let center = function (e) {
        console.log("核心代码", e, this);
      };

      ipt.oninput = debounce(center, 500);

      function debounce(fn, time) {
        let timer = null;
        // 下面的函数才是事件函数
        return function (e) {
          clearTimeout(timer);

          // 如果一直输入，input事件就会一直触发  直到停止之后才开始重新计时
          timer = setTimeout(() => {
            fn.call(this, e);
          }, time);
        };
      }
    </script>
  </body>
</html>
